package com.itouch.bracelet.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.itouch.bracelet.R;
import com.itouch.bracelet.bean.Medication;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

/**
 * cocoaSJ
 */
public class MedicationAdapter extends BaseAdapter 
		 {
	private ArrayList<Medication> list;
	private Context context;
	private ImageLoader  imageLoader = ImageLoader.getInstance();
	private LayoutInflater infalter;
	private DisplayImageOptions options;
	private boolean ismother;
	
	public MedicationAdapter(Context context, ArrayList<Medication> list,boolean ismother) {
		this.list = list;
		this.context = context;
		this.ismother=ismother;
		  infalter = LayoutInflater.from(context);
		  options = new DisplayImageOptions.Builder()
			.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
			.showImageOnLoading(R.drawable.default_face)
			.showImageForEmptyUri(R.drawable.default_face)
			
			.displayer(new RoundedBitmapDisplayer(5))
			.showImageOnFail(R.drawable.default_face).cacheInMemory(true)
			.cacheOnDisc(true).considerExifParams(true)
			.bitmapConfig(Bitmap.Config.RGB_565).build();
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Medication getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View converView, ViewGroup viewGrop) {
		// TODO Auto-generated method stub
		ViewHolder vh = null;
		if (converView == null) {
			vh = new ViewHolder();
			converView =infalter.inflate(
					R.layout.adapter_item_sport, viewGrop,false);
			vh.start_time = (TextView) converView.findViewById(R.id.start_time);
			 vh.content = (TextView) converView.findViewById(R.id.content);
			 vh.next_time = (TextView) converView.findViewById(R.id.next_time);
			 vh.avat = (ImageView) converView.findViewById(R.id.avat);
			 vh.delete_list = (ImageView) converView.findViewById(R.id.delete_list);
			converView.setTag(vh);
		} else {
			vh = (ViewHolder) converView.getTag();
		}
		Medication bean = getItem(position);
		
		vh.start_time.setText(bean.getCreateTime()+"");
		vh.next_time.setText("下次服药时间:"+bean.getNextMedDate()+"");
		vh.content.setText(bean.getMedicationName()+"");
		vh.delete_list.setVisibility(View.VISIBLE);
		vh.delete_list.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				deleteCallback.delete(position);
				
			}
		});
		if(ismother){
			vh.avat .setImageResource(R.drawable.m_avatar);
		}else{
			vh.avat .setImageResource(R.drawable.c_avatar);
		}
		return converView;
	}
	
	private DeleteCallback deleteCallback;
	
	public DeleteCallback getDeleteCallback() {
		return deleteCallback;
	}

	public void setDeleteCallback(DeleteCallback deleteCallback) {
		this.deleteCallback = deleteCallback;
	}




	public interface DeleteCallback{
		public void delete(int position);
		
	}
	
	
	

//	private AddLinstener listener;
//
//	public void setListener(AddLinstener listener) {
//		this.listener = listener;
//	}
//
//
//
//	public interface AddLinstener{
//		
//		public void add(int position);
//	}
//	
	

	class ViewHolder {
		public ImageView avat;
		public ImageView delete_list;
		public TextView start_time;
		public TextView next_time;
		public TextView content;
	}

}
